package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CaricatureActivity;
import flc.ast.activity.CaricatureDetailActivity;
import flc.ast.adapter.HomeCaricatureAdapter;
import flc.ast.adapter.TodayRecAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private TodayRecAdapter recAdapter;
    private HomeCaricatureAdapter caricatureAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvTypeAll.setOnClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recAdapter = new TodayRecAdapter();
        mDataBinding.rvRecommendList.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(this);

        mDataBinding.rvCaricatureList.setLayoutManager(new LinearLayoutManager(mContext));
        caricatureAdapter = new HomeCaricatureAdapter();
        mDataBinding.rvCaricatureList.setAdapter(caricatureAdapter);
        caricatureAdapter.addChildClickViewIds(R.id.ivHomeCaricatureItemCollect);
        caricatureAdapter.setOnItemClickListener(this);
        caricatureAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvTypeAll://查看全部
                startActivity(CaricatureActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        CaricatureDetailActivity.stkResBean = bean;
        startActivity(CaricatureDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp == null) listSp = new ArrayList<>();
        switch (view.getId()) {
            case R.id.ivHomeCaricatureItemCollect://收藏
                if (caricatureAdapter.getItem(position).isSelected()) {
                    for (StkResBean listDatum : listSp) {
                        if (listDatum.getThumbUrl().equals(caricatureAdapter.getItem(position).getThumbUrl())) {
                            listSp.remove(listDatum);
                            break;
                        }
                    }
                } else {
                    listSp.add(0, caricatureAdapter.getItem(position));
                }
                AppSpUtils.saveCollectList(listSp);
                caricatureAdapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    protected void initData() {
        getTodayRecData();
        getCaricatureData();
    }

    @Override
    public void onResume() {
        super.onResume();
        caricatureAdapter.notifyDataSetChanged();
    }

    private void getTodayRecData() {
        StkResApi.getTagResourceList(this, AppConfig.TODAY_RECOMMEND_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getCaricatureData() {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + "Pq1QcdCEiOL", StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        caricatureAdapter.setList(result);
                    }
                }
            }
        });
    }

}